import iconFont from '@/assets/iconFont/iconfont.less';
import { Button, ConfigProvider, theme } from 'antd';
import type { ButtonProps } from 'antd/es/button';
import cls from 'classnames';
import React from 'react';

const { useToken } = theme;

export type HandleBtnProps = {
  handleType?: string;
} & ButtonProps;

const HandleBtn: React.FC<HandleBtnProps> = ({ handleType, icon, ...rest }) => {
  const { token } = useToken();
  let color = token.colorPrimary;

  const code = () => {
    let unicode;
    switch (handleType) {
      case 'add':
        unicode = '&#xe8cb;';
        break;
      case 'delete':
        unicode = '&#xe8cf;';
        break;
      case 'import':
        unicode = '&#xe8d1;';
        break;
      case 'export':
        unicode = '&#xe8d2;';
        break;
      case 'download':
        unicode = '&#xe8ca;';
        break;
      case 'enable':
        unicode = '&#xe8cc;';
        break;
      case 'disabled':
        unicode = '&#xe8ce;';
        break;
      case 'parameter':
        unicode = '&#xe8ab;';
        break;
      case 'sum':
        unicode = '&#xe8d0;';
        break;
      case 'enlarg':
        unicode = '&#xe8a9;';
        break;
      default:
        unicode = '';
    }
    return unicode;
  };

  if (handleType === 'delete' || handleType === 'disabled') {
    color = token.colorError;
  }

  return (
    <ConfigProvider
      theme={{
        components: {
          Button: {
            paddingInline: 6,
            paddingBlock: 0,
          },
        },
      }}
    >
      <Button
        {...rest}
        type="text"
        key="delete"
        icon={
          icon || (
            <span
              className={cls([iconFont.iconfont, 'anticon', 'btn-iconfont'])}
              style={{
                color,
                marginInlineEnd: 0,
              }}
              dangerouslySetInnerHTML={{ __html: code() }}
            ></span>
          )
        }
        style={{ height: 24 }}
      >
        {rest.children}
      </Button>
    </ConfigProvider>
  );
};
export default HandleBtn;
